package com.ruoyi.procurementmanagement.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.procurementmanagement.domain.EsProcurementContractProduct;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procurementmanagement.domain.EsProcurementContract;
import com.ruoyi.procurementmanagement.service.IEsProcurementContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购合同(es_procurement_contract)Controller
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/procurementmanagement/contract")
public class EsProcurementContractController extends BaseController
{
    @Autowired
    private IEsProcurementContractService esProcurementContractService;

    /**
     * 查询采购合同(es_procurement_contract)列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsProcurementContract esProcurementContract)
    {
        startPage();
        List<EsProcurementContract> list = esProcurementContractService.selectEsProcurementContractList(esProcurementContract);
        return getDataTable(list);
    }

    /**
     * 导出采购合同(es_procurement_contract)列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:export')")
    @Log(title = "采购合同(es_procurement_contract)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsProcurementContract esProcurementContract)
    {
        List<EsProcurementContract> list = esProcurementContractService.selectEsProcurementContractList(esProcurementContract);
        ExcelUtil<EsProcurementContract> util = new ExcelUtil<EsProcurementContract>(EsProcurementContract.class);
        util.exportExcel(response, list, "采购合同(es_procurement_contract)数据");
    }

    /**
     * 获取采购合同(es_procurement_contract)详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(esProcurementContractService.selectEsProcurementContractById(id));
    }

    /**
     * 新增采购合同(es_procurement_contract)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:add')")
    @Log(title = "采购合同(es_procurement_contract)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsProcurementContract esProcurementContract) {
        logger.info("新增合同数据: {}", esProcurementContract);
        if (esProcurementContract.getEsProcurementContractProductList() != null) {
            logger.info("包含商品明细数量: {}", esProcurementContract.getEsProcurementContractProductList().size());
        }
        return toAjax(esProcurementContractService.insertEsProcurementContract(esProcurementContract));
    }

    /**
     * 修改采购合同(es_procurement_contract)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:edit')")
    @Log(title = "采购合同(es_procurement_contract)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsProcurementContract esProcurementContract) {
        logger.info("修改合同数据: {}", esProcurementContract);
        if (esProcurementContract.getEsProcurementContractProductList() != null) {
            logger.info("包含商品明细数量: {}", esProcurementContract.getEsProcurementContractProductList().size());
        }
        return toAjax(esProcurementContractService.updateEsProcurementContract(esProcurementContract));
    }

    /**
     * 删除采购合同(es_procurement_contract)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:remove')")
    @Log(title = "采购合同(es_procurement_contract)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(esProcurementContractService.deleteEsProcurementContractByIds(ids));
    }

    /**
     * 更新合同状态
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:edit')")
    @Log(title = "采购合同(es_procurement_contract)", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/status")
    public AjaxResult updateStatus(@PathVariable("id") Integer id, @RequestBody Map<String, String> status) {
        String contractStatus = status.get("status");
        return toAjax(esProcurementContractService.updateContractStatus(id, contractStatus));
    }

    /**
     * 获取采购合同产品列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:contract:query')")
    @GetMapping(value = "/{id}/products")
    public AjaxResult getContractProducts(@PathVariable("id") Integer id) {
        List<EsProcurementContractProduct> productList = esProcurementContractService.selectEsProcurementContractProductList(id);
        return success(productList);
    }
}
